<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_TourMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Tour();
	}

	public function getTourById($tour_id) {
		$result = $this->_db_table->find($tour_id);

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		}

		$row = $result->current();
                

		$json = $row->toArray();

		return $json;
	}

	public function getTourMember($tour_id) {
		$this->db_user_tour = new Api_Model_DbTable_UserTour();
		$s_user = $this->db_user_tour->select()->from('user_tour', array('*'))
		->where('tour_id = ?', $tour_id)
                ->where('status = ?', '1');
		$re_user = $this->db_user_tour->fetchAll($s_user);
		foreach ($re_user as $row) {
			// $row should be a Zend_Db_Table_Row object
			// cast to array
			$rowArray = $row->toArray();

			$this->db_user = new Api_Model_DbTable_User();
			$select = $this->db_user->select()->from('users', array('*'))
			->where('user_id = ?', $rowArray['user_id']);
			$result = $this->db_user->fetchAll($select);
			$ru = $result->current();
			$ruArray = $ru->toArray();

			$user = array('user_id' => $ruArray['user_id'],
                            'username' => $ruArray['username'],
                            'fullname' => $ruArray['fullname'],
                            'profile_picture' => $ruArray['profile_picture']);
			$json[] = array('user' => $user);

		}

		return $json;
	}

	public function getCheckInLocation($tour_id) {
		$this->db_tour_location = new Api_Model_DbTable_TourLocation();
		$s_lo = $this->db_tour_location->select()->from('tour_location', array('*'))
		->where('tour_id = ?', $tour_id);
		$re_lo = $this->db_tour_location->fetchAll($s_lo);
		if(count($re_lo) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($re_lo as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$this->db_location = new Api_Model_DbTable_Location();
				$select = $this->db_location->select()->from('locations', array('*'))
				->where('location_id = ?', $row->location_id);
				$result = $this->db_location->fetchAll($select);
				$rl = $result->current();
				$rlArray = $rl->toArray();

				$json[] = array('location' => $rlArray);

			}

			return $json;
		}
	}

	public function getAllLocationTour($location_id) {
		$select = $this->_db_table->select()->setIntegrityCheck(false)->from(array('t' => 'tours'), array('t.*'))
		->joinInner(array('tl' => 'tour_location'), 't.tour_id = tl.tour_id')
		->where('tl.location_id = ?', $location_id);
		$result = $this->_db_table->fetchAll($select);

		//        date_default_timezone_set('Asia/Ho_Chi_Minh');
		//        $currentDateTime = date('Y-m-d H-i-s');

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$rowArray = $row->toArray();

				//count
				$this->db_photo = new Api_Model_DbTable_Photo();
				$s_photo = $this->db_photo->select()->from('photos', array('*'))->where('tour_id = ?', $row->tour_id);
				$re_photo = $this->db_photo->fetchAll($s_photo);
				$count_photo = count($re_photo);

				//count
				$this->db_user_tour = new Api_Model_DbTable_UserTour();
				$s_user = $this->db_user_tour->select()->from('user_tour', array('*'))
				->where('tour_id = ?', $row->tour_id);
				$re_user = $this->db_user_tour->fetchAll($s_user);
				$count_user = count($re_user);

				$json[] = array('tour' => array('tour' => $rowArray,
                            'tour_count' => array('count_photo' => $count_photo, 'count_user' => $count_user)));

			}
			return $json;
		}
	}

	public function getAllUserTour($user_id) {
		$select = $this->_db_table->select()->setIntegrityCheck(false)->from(array('t' => 'tours'), array('t.*'))
		->joinInner(array('ut' => 'user_tour'), 't.tour_id = ut.tour_id')
		->where('ut.user_id = ?', $user_id);
		$result = $this->_db_table->fetchAll($select);


		//        date_default_timezone_set('Asia/Ho_Chi_Minh');
		//        $currentDateTime = date('Y-m-d H-i-s');

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				$selectstt = $this->_db_table->select()->from('tours', array('*'))
				->where('tour_id = ?', $row->tour_id);
				$resultstt = $this->_db_table->fetchAll($selectstt);
				$row_stt = $resultstt->current();
				$row->status = $row_stt->status;

				$rowArray = $row->toArray();

				//count
				$this->db_photo = new Api_Model_DbTable_Photo();
				$s_photo = $this->db_photo->select()->from('photos', array('*'))->where('tour_id = ?', $row->tour_id);
				$re_photo = $this->db_photo->fetchAll($s_photo);
				$count_photo = count($re_photo);

				//count
				$this->db_user_tour = new Api_Model_DbTable_UserTour();
				$s_user = $this->db_user_tour->select()->from('user_tour', array('*'))
				->where('tour_id = ?', $row->tour_id);
				$re_user = $this->db_user_tour->fetchAll($s_user);
				$count_user = count($re_user);

				$s_role = $this->db_user_tour->select()->from('user_tour', array('*'))
				->where('tour_id = ?', $row->tour_id)->where('user_id = ?', $user_id);
				$re_role = $this->db_user_tour->fetchAll($s_role);
				$row_role = $re_role->current();

				$json[] = array('tour' => array('tour' => $rowArray,
                            'tour_count' => array('count_photo' => $count_photo, 'count_user' => $count_user),
                            'role' => $row_role->role));


			}
			return $json;
		}
	}

	public function createTour($data){

		$db_user_tour = new Api_Model_DbTable_UserTour();
		//$db_tour_location = new Api_Model_DbTable_TourLocation();

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');
		//check stt cua tour
		if($data['tour']['start_date'] > $currentDateTime){
			$data['tour']['status'] = '0';
		} elseif($data['tour']['end_date'] < $currentDateTime){
			$data['tour']['status'] = '2';
		} elseif($data['tour']['start_date'] < $currentDateTime && $data['tour']['end_date'] > $currentDateTime){
			$data['tour']['status'] = '1';
		}
		$affectedRow = $this->_db_table->insert($data['tour']);

		if ($affectedRow){
			$tour_id = $this->_db_table->getAdapter()->lastInsertId();
			$this->_db_table->update(array('created_time' => $currentDateTime),
			array('tour_id = ?' => $tour_id));
			//insert vao bang user_tour
			$user_tour = array(
                'tour_id' => $tour_id,
                'user_id' => $data['user'],
                'role' => '1',
                'status' => '2'
                );
                $db_user_tour->insert($user_tour);
                $user_tour_id = $db_user_tour->getAdapter()->lastInsertId();
                $db_user_tour->update(array('created_time' => $currentDateTime),
                array('user_tour_id = ?' => $user_tour_id));

                $json = array('tour_id' => $tour_id);
                return $json;

		} else {
			throw new Exception('Internal Server Error' ,500);
		}
	}

	public function updateTour($data, $tour_id){

		$select = $this->_db_table->select()->from('tours', array('*'))->where('tour_id = ?', $tour_id);
		$result_tour = $this->_db_table->fetchAll($select);

		$this->db_user = new Api_Model_DbTable_UserTour();
		$se = $this->db_user->select()->from('user_tour', array('*'))
		->where('user_id = ?', $data['user_id']);
		$result = $this->db_user->fetchAll($se);
		$ru = $result->current();
		$ruArray = $ru->toArray();

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');

		if (count($result_tour) != 0){
			if($ruArray['role'] == "1"){
				$where = array('tour_id = "'.$tour_id.'"');
				$this->_db_table->update($data['tour'], $where);
				$this->_db_table->update(array('updated_time' => $currentDateTime),
				array('tour_id = ?' => $tour_id));

				$s = $this->_db_table->select()->from('tours', array('*'))->where('tour_id = ?', $tour_id);
				$re= $this->_db_table->fetchAll($s);
				$ro = $re->current();

				$return = array('tour' => $ro->toArray());
				return $return;

			} else{
				throw new Exception(json_encode(array('meta' => 'Not Acceptable')),406);
			}
		} else {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		}

	}

	public function checkin($data){

		$this->db_tour_location = new Api_Model_DbTable_TourLocation();
		//        date_default_timezone_set('Asia/Ho_Chi_Minh');
		//        $currentDateTime = date('Y-m-d H-i-s');
		$checkinlocation = array('tour_id' => $data['tour_id'],
                        'user_id' => $data['user_id'],
                        'location_id' => $data['location_id']);

		$this->db_tour_location->insert($checkinlocation);


	}
        
        
        public function inviteUser($data, $tour_id){

		$this->db_user_tour = new Api_Model_DbTable_UserTour();
		//        date_default_timezone_set('Asia/Ho_Chi_Minh');
		//        $currentDateTime = date('Y-m-d H-i-s');
		$inviteuser = array('tour_id' => $tour_id,
                        'user_id' => $data['user_id'],
                        'role' => '0',
                        'status' => '0');

		$this->db_user_tour->insert($inviteuser);


	}

	public function checkUser($tour_id){

		$this->db_user_tour = new Api_Model_DbTable_UserTour();
		$s_user = $this->db_user_tour->select()->from('user_tour', array('*'))
		->where('tour_id = ?', $tour_id);
		$re_user = $this->db_user_tour->fetchAll($s_user);
		foreach ($re_user as $row) {
			$json[] = array('user' => array('user_id' => $row->user_id, 'status' => $row->status));
		}

		return $json;


	}

	public function joinTour($data, $tour_id){

		$this->db_user_tour = new Api_Model_DbTable_UserTour();
		//        date_default_timezone_set('Asia/Ho_Chi_Minh');
		//        $currentDateTime = date('Y-m-d H-i-s');
		$join = array('tour_id' => $tour_id,
                        'user_id' => $data['user'],
                        'role' => '0',
                        'status' => '1');

		$this->db_user_tour->insert($join);

	}
        
        public function outTour($data, $tour_id){

		$this->db_user_tour = new Api_Model_DbTable_UserTour();
                $a = $this->db_user_tour->delete(array('user_id = ?' => $data['user_id'], 'tour_id = ?' => $tour_id));

	}

	public function answer($data, $tour_id){

		$this->db_user_tour = new Api_Model_DbTable_UserTour();
		if($data['answer'] == 'accept'){
			$where = array('user_id = ?' => $data['user'], 'tour_id = ?' => $tour_id);
			$this->db_user_tour->update(array('status' => '2'), $where);
		} elseif ($data['answer'] == 'deny'){
			$where = array('user_id = ?' => $data['user_id'], 'tour_id = ?' => $tour_id);
			$this->db_user_tour->delete($where);
		}

	}

}

